package com.molichuxing.gateway.bff.car.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.UploadFileRequestDto;
import com.molichuxing.framework.dto.UploadFileResponseDto;
import com.molichuxing.framework.result.ResponseCodeEnum;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.framework.utils.*;
import com.molichuxing.gateway.bff.basic.services.FileStorageBffService;
import com.molichuxing.gateway.bff.car.convertor.Convert;
import com.molichuxing.gateway.bff.car.entity.request.modify.CarDetailModifyVo;
import com.molichuxing.gateway.bff.car.entity.response.*;
import com.molichuxing.gateway.bff.car.services.CarBffService;
import com.molichuxing.gateway.bff.car.services.CarFenceExceptionBffService;
import com.molichuxing.gateway.property.PowerTypeEnum;
import com.molichuxing.gateway.property.StockStatusEnum;
import com.molichuxing.gateway.utils.PoiExcelUtil;
import com.molichuxing.gateway.utils.entity.UserEntity;
import com.molichuxing.services.business.dto.request.create.VehicleCreateBizDto;
import com.molichuxing.services.business.dto.response.*;
import com.molichuxing.services.business.service.*;
import com.molichuxing.services.infrastructure.dto.request.create.StockCarCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.CarDetailModifyDto;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.infrastructure.service.*;
import com.molichuxing.services.property.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 车辆bff
 * @author xuelin yi
 */
@Service("carFenceExceptionBffService")
public class CarFenceExceptionBffServiceImpl implements CarFenceExceptionBffService {

	private static final Logger logger = LoggerFactory.getLogger(CarFenceExceptionBffServiceImpl.class);

	@Resource
	private CarFenceExceptionBizService carFenceExceptionBizService;

	@Resource
	private StockStoreService stockStoreService;

	@Resource
	private VehicleBizService vehicleBizService;

	@Resource
	private StockCarBizService stockCarBizService;

	@Resource
	private StoreBizService storeBizService;

	@Resource
	private FenceStoreStockService fenceStoreStockService;

	@Resource
	private IovFenceService iovFenceService;

	@Override
	public Paged<CarsFenceExceptionVo> getCarsFenceExceptionPaged(Map<String, Object> params, Integer pageNum, Integer pageSize) {
		if(null != params.get("fenceName")) {
			List<IovFenceDto> iovFenceListDto = iovFenceService.getVagueFenceByName(ObjectCastUtil.castString(params.get("fenceName")));
			if((null != iovFenceListDto)&&(iovFenceListDto.size()>0)) {
				params.put("fenceIds",iovFenceListDto.stream().map(IovFenceDto::getId).collect(Collectors.toList()));
			}
		}
		/*Integer stockType = ObjectCastUtil.castInteger(params.get("stockType"));
		Integer stockId = ObjectCastUtil.castInteger(params.get("stockId"));
		if((null != stockType)&&(StockCarStockTypeEnum.STORE.getValue() == stockType)&&null != stockId) {
			StockStoreDto stockStoreDto = stockStoreService.getStockStoreByStoreId(stockId);
			if(stockStoreDto==null) {
				params.put("stockId", 0);
			}else {
				params.put("stockId", stockStoreDto.getStockId());
			}
		}*/
		Paged<CarFenceExceptionBizDto> paged = carFenceExceptionBizService.getCarsFenceExceptionPaged(params, pageNum, pageSize);
		Paged<CarsFenceExceptionVo> page = Convert.toCarFenceExceptionVoPage(paged);
		if(null != page && null != page.getList()) {
			getCarName(page.getList());
			getVehicleStock(page.getList());
		}
		return page;
	}

	@Override
	public CarsFenceExceptionDetailVo getCarsFenceExceptionDetail(Integer id) {
		CarsFenceExceptionDetailVo carsFenceExceptionDetailVo = null;
		CarFenceExceptionBizDto carFenceExceptionBizDto = carFenceExceptionBizService.getCarsFenceExceptionDetail(id);
		if(null == carFenceExceptionBizDto) {
			return carsFenceExceptionDetailVo;
		}
		carsFenceExceptionDetailVo = Convert.toCarsFenceExceptionDetailVo(carFenceExceptionBizDto);
		StockCarBaseBizDto stockCarBizDto = stockCarBizService.getStockBaseCar(carFenceExceptionBizDto.getCarId());
		if(null != stockCarBizDto) {
			List<Integer> fenceIds = new ArrayList<>();
			if (stockCarBizDto.getStockType() == StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue()) {
				carsFenceExceptionDetailVo.setStockName(stockCarBizDto.getStockName());
				fenceIds = fenceStoreStockService.getFenceByStoreId(stockCarBizDto.getStockId(),FenceStoreStockTypeEnum.ENTREPOT_STORAGE);
			} else {
				try{
					DealerStoreInfoBizDto dealerStoreInfoBizDto = storeBizService.getDealerStoreInfo(stockCarBizDto.getStoreId());
					carsFenceExceptionDetailVo.setStockName(dealerStoreInfoBizDto.getDealerShortName() + "-" + dealerStoreInfoBizDto.getStoreShortName());
					fenceIds = fenceStoreStockService.getFenceByStoreId(stockCarBizDto.getStoreId(),FenceStoreStockTypeEnum.STORE);
				}catch(Exception e) {
					logger.error("查询门店信息异常");
				}
			}
			//获取门店下所有围栏
			if((null != fenceIds)&&(fenceIds.size() > 0)) {
				List<IovFenceDto> iovFenceDtoList = iovFenceService.getFenceList(fenceIds);
				if((null != iovFenceDtoList)&&(iovFenceDtoList.size() > 0)) {
					carsFenceExceptionDetailVo.setOpFenceNameList(iovFenceDtoList.stream().map(IovFenceDto::getFenceName).collect(Collectors.toList()));
				}
			}
			//获取车辆所在的围栏信息
			Map<String,List<IovFenceDto>> iovFenceDtoMap = iovFenceService.getFenceByVins(Arrays.asList(carsFenceExceptionDetailVo.getVin()));
			if(null != iovFenceDtoMap && null != iovFenceDtoMap.get(carsFenceExceptionDetailVo.getVin())&& iovFenceDtoMap.get(carsFenceExceptionDetailVo.getVin()).size() > 0) {
				carsFenceExceptionDetailVo.setIovFenceName(iovFenceDtoMap.get(carsFenceExceptionDetailVo.getVin()).get(0).getFenceName());
			}
		}
		return carsFenceExceptionDetailVo;
	}

	private Map<Integer, String> getCarName(List<CarsFenceExceptionVo> carDtoList) {
		Map<Integer, String> result = null;
		if(null != carDtoList && carDtoList.size() > 0) {
			Set<Integer> ids = new HashSet<>();
			for(CarsFenceExceptionVo carDto : carDtoList) {
				ids.add(carDto.getVehicleId());
			}
			List<Integer> vehicleIds=new ArrayList<>(ids);
			result = vehicleBizService.getVehicleFullName(vehicleIds);
		}
		if((null != result)){
			for(CarsFenceExceptionVo carsFenceExceptionVo : carDtoList) {
				if(null != result.get(carsFenceExceptionVo.getVehicleId())) {
					carsFenceExceptionVo.setVehicleName(result.get(carsFenceExceptionVo.getVehicleId()));
				}
			}
		}
		return result;
	}


	private void getVehicleStock(List<CarsFenceExceptionVo> carDtoList){
		if(null != carDtoList && carDtoList.size() > 0) {
			List<Integer> ids = new ArrayList<>();
			List<Integer> fenceIds = new ArrayList<>();
			for (CarsFenceExceptionVo carDto : carDtoList) {
				ids.add(carDto.getCarId());
				fenceIds.add(carDto.getFenceId());
			}
			//获取围栏信息
			List<IovFenceDto> iovFenceDtoList = iovFenceService.getFenceList(fenceIds);
			Map<Integer, IovFenceDto> iovFenceMap = null;
			if((null != iovFenceDtoList)&&(iovFenceDtoList.size() > 0)) {
				iovFenceMap = iovFenceDtoList.stream().collect(Collectors.toMap(IovFenceDto::getId, a -> a, (k1, k2) -> k1));
			}
			//获取库存信息
			Map<Integer, StockCarBizDto> stockCarBizMap = stockCarBizService.getStockBaseCars(ids);
			if(null == stockCarBizMap) {
				return;
			}
			for (CarsFenceExceptionVo carDto : carDtoList) {
				StockCarBizDto stockCarBizDto = stockCarBizMap.get(carDto.getCarId());
				if(null != stockCarBizDto) {
					if (stockCarBizDto.getStockType() == StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue()) {
						carDto.setStockName(stockCarBizDto.getStockName());
					} else {
						try{
							DealerStoreInfoBizDto dealerStoreInfoBizDto = storeBizService.getDealerStoreInfo(stockCarBizDto.getStoreId());
							carDto.setStockName(dealerStoreInfoBizDto.getDealerShortName() + "-" + dealerStoreInfoBizDto.getStoreShortName());
						}catch(Exception e) {
							logger.error("查询门店信息异常");
						}
					}
				}
				if(null != iovFenceMap) {
					IovFenceDto iovFenceDto = iovFenceMap.get(carDto.getFenceId());
					if(null != iovFenceDto) {
						carDto.setFenceName(iovFenceDto.getFenceName());
					}
				}


			}
		}
	}


}
